<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8" />
    <title>机器人</title>
    <link
      rel="stylesheet"
      href="/app/admin/component/layui/css/layui.css?v=2.8.12"
    />
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
    <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
  </head>
  <body class="pear-container">
    <div class="layui-card">
      <div class="layui-card-body">
        <table id="bot-table" lay-filter="bot-table"></table>
      </div>
    </div>
  </body>
  <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
  <script src="/app/admin/component/pear/pear.js"></script>
  <script src="/app/admin/admin/js/permission.js"></script>
  <script src="/app/admin/admin/js/common.js"></script>
  <script type="text/html" id="table-toolbar">
    <button
      class="pear-btn pear-btn-primary pear-btn-md"
      lay-event="add"
      permission="admin.bot.create"
    >
      <i class="layui-icon layui-icon-add-1"></i>创建机器人
    </button>
  </script>
  <script type="text/html" id="bot-row-op">
    <div class="layui-btn-container">
      <button type="button" class="layui-btn layui-btn-xs layui-bg-blue"  lay-event="settings">
        <i class="layui-icon layui-icon-set"></i>设置
      </button>
      <button type="button" class="layui-btn layui-btn-xs"  lay-event="edit">
        <i class="layui-icon layui-icon-edit"></i>编辑
      </button>
      <button type="button" class="layui-btn layui-btn-xs layui-bg-red"  lay-event="delete">
        <i class="layui-icon layui-icon-delete"></i>删除
      </button>
    </div>
  </script>
  <script type="text/html" id="field-enable-tpl">
    <input type="checkbox" name="enable" value="{{= d.enable }}"
    title="开启|禁用" lay-skin="switch" lay-filter="field-enable" {{= d.enable ?
    "checked" : "" }} data-id="{{= d.id}}" disabled>
  </script>
  <script>
    var GET_URL = "/admin/bot/index?bot_id=<?= request()->get('bot_id', 0);?>",
      ADD_URL = "/admin/bot/create",
      EDIT_URL = "/admin/bot/edit",
      DELETE_URL = "/admin/bot/delete";
    layui.use(["table", "common", "layer", "jquery", "form", "popup"], function () {
      var table = layui.table,
        layer = layui.layer,
        common = layui.common,
        $ = layui.$,
        form = layui.form;
      var bot_id = <?= request()->get('bot_id', 0);?>, is_master_bot = bot_id > 0;
      var table_cols = [
        {
          title: "ID",
          field: "id",
        },
        {
          title: "名称",
          field: "name",
        },
        {
          title: "备份机器人",
          field: "backups_count",
          hide: is_master_bot,
          templet: function(d){
            return '<div class="preview-backups" data-id="'+d.id+'"><a href="javascript:void(0);">'+d.backups_count+'</a></div>';
          }
        },
        {
          title: "备份目标",
          field: "backup_user_id",
          hide: !is_master_bot,
        },
        {
          title:
            '状态 <i class="layui-icon layui-icon-about layer-tips" data-tip="开启后可以才可以接收回复消息"></i>',
          field: "enable",
          templet: "#field-enable-tpl",
          hide: is_master_bot,
        },
        {
          title:
            'hook状态 <i class="layui-icon layui-icon-about layer-tips" data-tip="机器人hook地址是否设置正确"></i>',
          field: "enable",
          templet: function(d){
            return '<div class="hook_status" data-id="'+d.id+'"><a href="javascript:void(0);">查看</a></div>'
          },
          hide: is_master_bot,
        },
        {
          title: "创建时间",
          field: "created_at",
        },
        {
          title: "操作",
          fixed: "right",
          toolbar: "#bot-row-op",
          hide: is_master_bot,
        },
      ];
      table.render({
        elem: "#bot-table",
        url: GET_URL,
        page: true,
        cols: [table_cols],
        skin: "line",
        size: "lg",
        toolbar: !is_master_bot ? "#table-toolbar" : true ,
        defaultToolbar: [
          {
            title: "刷新",
            layEvent: "refresh",
            icon: "layui-icon-refresh",
          },
          "filter",
          "exports",
        ],
      });

      //
      var add_layer = null;
      table.on("toolbar(bot-table)", function (obj) {
        if (obj.event === "add") {
          if(add_layer) return;
          layer.open({
            type: 2,
            title: "创建机器人",
            shade: 0.1,
            maxmin: true,
            area: [
              common.isModile() ? "100%" : "800px",
              common.isModile() ? "100%" : "500px",
            ],
            content: ADD_URL,
          });
        } else if (obj.event === "refresh") {
          refreshTable();
        }
      });
      // 
      var edit_layer = null;
      var settings_layer = null;
      table.on('tool(bot-table)', function(obj){
        if(obj.event == 'edit'){
          if(edit_layer) layer.close(edit_layer);
          edit_layer = layer.open({
            type: 2,
            title: "编辑机器人",
            shade: 0.1,
            maxmin: true,
            area: [
              common.isModile() ? "100%" : "800px",
              common.isModile() ? "100%" : "500px",
            ],
            content: EDIT_URL + '?id='+obj.data.id,
          });
        }else if(obj.event == 'delete'){
          layer.confirm('确认删除机器人(ID:'+obj.data.id+')?', {
            btn: ['确定', '关闭'] //按钮
          }, function(){
            var loading = layer.msg('提交中,请勿刷新页面', {
              icon: 16,
              shade: 0.3,
              time: 0,
            });
            $.ajax({
              url: DELETE_URL + '?id='+obj.data.id,
              method:'POST',
              data: {},
              success: function(data){
                if(data.code == 0){
                  return layui.popup.success("删除成功", function () {
                    refreshTable();
                  });
                }else{
                  return layui.popup.failure(data.msg || '新增失败');
                }
              },
              complete: function(){
                layer.close(loading);
              },
              error:function(){
                return layui.popup.failure('操作失败,请重试');
              }
            })
          }, function(index){
            layer.close(index);
          });
        }else if(obj.event == 'settings'){
          if(settings_layer) layer.close(settings_layer);
          settings_layer = layer.open({
            type: 2,
            title: "设置机器人",
            shade: 0.1,
            maxmin: true,
            area: [
              common.isModile() ? "100%" : "80%",
              common.isModile() ? "100%" : "80%",
            ],
            content: '/admin/bot/settings?id='+obj.data.id,
          });
        }
      });
      var tip_layer = null;
      $(document)
        .on("mouseenter", ".layer-tips", function () {
          tip_layer && layer.close(tip_layer);
          var tips = $(this).attr("data-tip");
          if (tips) {
            tip_layer = layer.tips(tips, this, {
              tips: 1,
            });
          }
        })
        .on("mouseleave", ".layer-tips", function () {
          tip_layer && layer.close(tip_layer);
        });
      //
      form.on("switch(field-enable)", function (obj) {});
      //
      var preview_backups_layer = null;
      $(document).on('click', '.preview-backups', function(){
        var bot_id = $(this).attr('data-id');
        if(preview_backups_layer) layer.close(preview_backups_layer);
        preview_backups_layer =layer.open({
          type: 2,
          title: "查看备份机器人",
          shade: 0.1,
          maxmin: true,
          area: [
            common.isModile() ? "100%" : "90%",
            common.isModile() ? "100%" : "80%",
          ],
          content: '/admin/bot/index?bot_id='+bot_id,
        });
      });
      // 
      $(document).on('click', '.hook_status', function(){
        var id = $(this).attr('data-id'), elem = $(this);
        $(this).html('<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>');
        $.ajax({
          url:'/admin/bot/status',
          data:{id:id},
          method:'GET',
          success: function(data){
            if(data.code == 0){
              if(data.data === true){
                elem.html('<i class="layui-icon layui-icon-ok layui-font-green"></i>')
              }else{
                elem.html('<i class="layui-icon layui-icon-close layui-font-gray"></i>')
              }
            }else{
              elem.html('<a href="javascript:void(0);">查看</a>');
              return layui.popup.failure(data.msg || '加载失败');
            }
          },
          error: function(){
            return layui.popup.failure('加载失败,请重试');
          }
        })
      });
      // 
      window.refreshTable = function () {
        table.reloadData("bot-table");
      };
    });
  </script>
</html>
